package com.gy.hsim.bservice.asyncload.thread;

import java.io.File;
import java.text.SimpleDateFormat;

import com.gy.hsim.bservice.asyncload.constants.CharRecordConstant;

public class AgingChatRecordThread extends Thread {
	
	private int agingDay = 0;
	
	private boolean needAging = false;
	
	private File folder;
	
	public AgingChatRecordThread(boolean needAgingChatRecordData,int agingChatRecordDays){
		this.needAging = needAgingChatRecordData;
		this.agingDay = agingChatRecordDays;
		folder = new File(CharRecordConstant.CHATRECORD_PATH);
		if(!folder.exists()) {
			folder.mkdir();
		}
	}

	public void run() {
		
		File files[] = folder.listFiles();
		
		if(files != null && files.length > 0) {
			for(File file : files) {
				
				long createFileTime = Long.parseLong(new SimpleDateFormat(
						"yyyyMMdd").format(file.lastModified()));
				
				long currentDay =  Long.parseLong(new SimpleDateFormat(
						"yyyyMMdd").format(System.currentTimeMillis()));
				
				if(needAging && currentDay - createFileTime > agingDay) {
					file.delete();
				}
			}
		} else {
			try {
				sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		try {
			sleep(2000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}

}
